package com.congee02.map;

import java.util.*;

/**
 * @author congee(congee02 @ 163.com)
 * @date 1/25/2023 10:28 AM
 */
public class BasicMap<K, V> extends AbstractMap<K, V> implements Map<K, V> {

    Set<Entry<K, V>> entrySet = new HashSet<>();

    @Override
    public Set<Entry<K, V>> entrySet() {
        return entrySet;
    }

    @Override
    public V put(K key, V value) {
        V oldValue = null;

        if (key == null) {
            for (Entry<K, V> e : entrySet) {
                if (e.getKey() == null) {
                    oldValue = e.getValue();
                    e.setValue(value);
                }
            }
        } else {
            for (Entry<K, V> e : entrySet) {
                if (key.equals(e.getKey())) {
                    oldValue = e.getValue();
                    e.setValue(value);
                }
            }
        }

        if (oldValue == null) {
            entrySet.add(new SimpleEntry<>(key, value));
        }

        return oldValue;
    }
}
